package cn.doitedu.demo;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/3/10
 **/

@Slf4j
public class ElasticOperateTest {
    public static void main(String[] args) throws Exception {
        CreateIndexRequest request = new CreateIndexRequest("events_static");
        request.settings(Settings.builder()
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 1)
        );

        /**
         * request.mapping(
         *         "{\n" +
         *         "  \"properties\": {\n" +
         *         "    \"message\": {\n" +
         *         "      \"type\": \"text\"\n" +
         *         "    }\n" +
         *         "  }\n" +
         *         "}",
         *         XContentType.JSON);
         *  或者
         *
         *  Map<String, Object> message = new HashMap<>();
         *  message.put("type", "text");
         *  Map<String, Object> properties = new HashMap<>();
         *  properties.put("message", message);
         *  Map<String, Object> mapping = new HashMap<>();
         *  mapping.put("properties", properties);
         *  request.mapping(mapping);
         *
         *  或者
         */
        XContentBuilder builder = jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("uid");
                {
                    builder.field("type", "text");
                }
                builder.endObject();

                builder.startObject("eventId");
                {
                    builder.field("type", "text");
                }
                builder.endObject();

                builder.startObject("eventCount");
                {
                    builder.field("type", "text");
                }
                builder.endObject();

            }
            builder.endObject();

        }
        builder.endObject();
        request.mapping(builder);

        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("doitedu01", 9200, "http")));

        // CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        // System.out.println(createIndexResponse);

        /**
         * 添加数据
         */
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("uid", "kimchy");
        jsonMap.put("eventId", "addCart");
        jsonMap.put("eventCount", "10");

        IndexRequest indexRequest = new IndexRequest("events_static").source(jsonMap);
        try {
            // 执行得到 response
             IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
             System.out.println("出参：" + indexResponse.toString());
            // 关闭
             client.close();
        } catch (Exception e) {

        }

        /**
         * 查询数据
         */
        // 根据文档id 查询
        GetRequest getRequest = new GetRequest("events_static", "p9L7HHgBMxZ2qblCMaUV");
        try {
            // 查询结果
            GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
            String id = documentFields.getId();// id
            Map<String, DocumentField> fields = documentFields.getFields();
            String index = documentFields.getIndex();// index
            Map<String, Object> source = documentFields.getSource();// 索引的字段信息
            long primaryTerm = documentFields.getPrimaryTerm();
            long version = documentFields.getVersion();
            System.out.println("id:" + id + "index:" + index + "version:" + version + "count:" + fields.size());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
